#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse
# 2018.03.21

black=$(tput setaf 0); red=$(tput setaf 1); green=$(tput setaf 2); yellow=$(tput setaf 3); blue=$(tput setaf 4); magenta=$(tput setaf 5); cyan=$(tput setaf 6); white=$(tput setaf 7); bold=$(tput bold); normal=$(tput sgr0); underline=$(tput smul); reset_underline=$(tput rmul); jiacu=${normal}${bold}

# 名称转换、挂载
function _file() {
bdisopathlower=$(echo "$bdisopath" | sed 's/[Ii][Ss][Oo]/iso/g')
file_title=$(basename "$bdisopathlower" .iso)
file_title_clean="$(echo "$file_title" | tr '[:space:]' '.')"
file_title_clean="$(echo "$file_title_clean" | sed s'/[.]$//')"
file_title_clean="$(echo "$file_title_clean" | tr -d '(')"
file_title_clean="$(echo "$file_title_clean" | tr -d ')')"
mkdir -p "/etc/inexistence/06.BluRay/$file_title_clean"
bdpath="/etc/inexistence/06.BluRay/$file_title_clean"
mount -o loop "$bdisopath" "$bdpath" >/dev/null 2>&1
echo -e "${normal}已将 ${cyan}$disk${normal} 以只读模式挂载到 ${cyan}$bdpath${normal}" ; }


# 多文件的操作
function _multi() {
ALLISO=` ls | tr " " "\n" | grep -Eo ".*\.[Ii][Ss][Oo]\>" `
ISONum=` ls | tr " " "\n" | grep -c ".*\.[Ii][Ss][Oo]\>" `
if [[ $ISONum == 0 ]]; then
    echo -e "${bold}${red}错误${jiacu} ${words}路径下不存在 ISO 文件，退出脚本 ...${normal}\n"
    exit 1
else
echo -e "${bold}${words}路径下有 ${green}${ISONum}${normal}${bold} 个 ISO 文件，开始挂载 ...${normal}\n"
    for disk in $ALLISO ; do
        bdisopath=`echo "$disk"` && _file
    done
    echo -e "\n${bold}完成，${green}${ISONum}${normal}${bold} 个 ISO 文件已全部挂载${normal}\n"
fi ; }


# 判断输入的类型
if [[ "$1" == ""  ]]; then
    echo -e "\n${bold}未输入路径，脚本将自动挂载当前工作路径下的所有 ISO 文件${normal}\n"
    words=当前 && _multi
elif [[ -d "$1" ]]; then
    echo -e "\n${bold}检测到文件夹，脚本将自动挂载该文件夹下的所有 ISO 文件${normal}\n"
    cd "$1"
    words=该 && _multi
elif [[ -e "$1" ]] && [[ ! -d "$1" ]]; then
    echo -e "\n${bold}检测到单个 ISO 文件，脚本将挂载该 ISO 文件${normal}\n"
    bdisopath=`echo "$1"` ; disk=`echo "$1"`
    _file && echo
elif [[ ! -e "$1" ]]; then
    echo -e "\n${bold}${red}错误${jiacu} 该文件或路径不存在，请重新输入！${normal}\n"
    exit 0
fi

